package com.yueman.ptoolopenticket.ticket.TicketService.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.yueman.ptoolopenticket.ticket.TicketService.CommonService;
import com.yueman.ptoolopenticket.ticket.Tpl.CommonTemplate;
import com.yueman.ptoolopenticket.ticket.aop.CommSignCheck;
import com.yueman.ptoolopenticket.ticket.dto.ResponseResult;
import com.yueman.ptoolopenticket.ticket.dto.Result;
import com.yueman.ptoolopenticket.ticket.dto.ResultBuilder;
import com.yueman.ptoolopenticket.ticket.dto.request.lixin.*;
import com.yueman.ptoolopenticket.ticket.dto.response.lixin.*;
import com.yueman.ptoolopenticket.ticket.mapper.TicketSubAcctRechargeRepository;
import com.yueman.ptoolopenticket.ticket.mapper.TicketTraceRepository;
import com.yueman.ptoolopenticket.ticket.mapper.TicketUserRepository;
import com.yueman.ptoolopenticket.ticket.pojo.SubAcctRecharge;
import com.yueman.ptoolopenticket.ticket.pojo.Trace;
import com.yueman.ptoolopenticket.ticket.pojo.User;
import com.yueman.ptoolopenticket.ticket.utils.Constant;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

@CommSignCheck
@Slf4j
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class CommonServiceimpl implements CommonService {
    public final Map<String, CommonTemplate> map;

    public final TicketSubAcctRechargeRepository subAcctRechargeRepository;

    public final TicketTraceRepository traceRepository;

    public final TicketUserRepository ticketUserRepository;

    public final TicketTraceRepository ticketTraceRepository;

    @Override
    public Result<UserRes> register(UserReq userReq) {
        return map.get(userReq.getChannel()).register(userReq);
    }

    @Override
    public Result<VerificationRes> VerificationCode(VerificationReq verificationReq) {
        return map.get(verificationReq.getChannel()).VerificationCode(verificationReq);
    }

    @Override
    public Result<SignRes> sign(SignReq signReq) {
        return map.get(signReq.getChannel()).sign(signReq);
    }

    @Override
    public Result<SignQueryRes> signQuery(SignQueryReq signQueryReq) {
        return map.get(signQueryReq.getChannel()).signQuery(signQueryReq);
    }

    @Override
    public Result<LoginRes> login(LoginReq loginReq) {
        return map.get(loginReq.getChannel()).login(loginReq);
    }

    @Override
    public Result<String> Province(ProvinceReq provinceReq) {
        return map.get(provinceReq.getChannel()).Province(provinceReq);
    }

    @Override
    public Result<String> City(CityReq cityReq) {
        return map.get(cityReq.getChannel()).City(cityReq);
    }

    @Override
    public Result<String> District(DistrictReq districtReq) {
        return map.get(districtReq.getChannel()).District(districtReq);
    }


    /**
     * 实名认证查询
     *
     * @param realNameAuthQueryReq
     * @return
     */
    @Override
    public Result<RealNameAuthQueryRes> rnameAuthQuery(RealNameAuthQueryReq realNameAuthQueryReq) {
        User user = ticketUserRepository.findUserByIdno(realNameAuthQueryReq.getIdNo());
        if (user != null) {
            String status = user.getStatus();
            if (StrUtil.equals(status, "1")) {
                RealNameAuthQueryRes realNameAuthQueryRes = new RealNameAuthQueryRes();
                realNameAuthQueryRes.setName(user.getUname());
                realNameAuthQueryRes.setIDNo(user.getIdno());
                realNameAuthQueryRes.setRetcode(Constant.SUCCESS_CODE);
                realNameAuthQueryRes.setRetMsg(Constant.AUTH_SUCCESS);
                return ResultBuilder.success(realNameAuthQueryRes);
            } else if (StrUtil.equals(status, "2")) {
                return ResultBuilder.fail(ResponseResult.AUTHFAIL);
            }
        } else {
            return ResultBuilder.fail(ResponseResult.NOTNAMEAUTH);
        }
        return map.get(realNameAuthQueryReq.getChannel()).rnameAuthQuery(realNameAuthQueryReq);
    }

    @Override
    public Result<InvoiceApplyRes> InvoiceApply(InvoiceApplyReq invoiceApplyReq) {
        return map.get(invoiceApplyReq.getChannel()).InvoiceApply(invoiceApplyReq);
    }

    @Override
    public Result<GoodsRes> GoodsDetails(GoodsReq goodsReq) {
        return map.get(goodsReq.getChannel()).GoodsDetails(goodsReq);
    }

    /**
     * 支付初始化
     *
     * @param payloadReq
     * @return
     */
    @Override
    public Result<PayRes> Payload(PayReq payloadReq) {

        return null;
    }

    /**
     * 账号查询
     *
     * @param subAcctQueryReq
     * @return
     */
    @Override
    public Result<SubAcctQueryRes> SubAcctQuery(SubAcctQueryReq subAcctQueryReq) {
        return map.get(subAcctQueryReq.getChannel()).SubAcctQuery(subAcctQueryReq);
    }

    /**
     * 充值
     *
     * @param rechargeReq
     * @return
     */
    @Override
    public Result<RechargeRes> Recharge(RechargeReq rechargeReq) {
        String rechargeNo = rechargeReq.getRechargeNo();
        Result<RechargeRes> rechargeResResult = map.get(rechargeReq.getChannel()).Recharge(rechargeReq);
        log.info("retCode:{}",rechargeResResult.getRetCode());
        if (StrUtil.equals(rechargeResResult.getRetCode(), ResponseResult.SUCCESS.getStates())) {
            RechargeRes rechargeRes = rechargeResResult.getBody();
            SubAcctRecharge subAcctRecharge = new SubAcctRecharge();
            subAcctRecharge.setOrdernumber("cz" + rechargeRes.getRechargeNo());
            subAcctRecharge.setRechargeStatus("0");
            subAcctRecharge.setDownOrderNumber(rechargeNo);
            subAcctRecharge.setChannel(rechargeReq.getChannel());
            subAcctRecharge.setTranAmount(rechargeReq.getAmount());
            subAcctRecharge.setSubName(rechargeReq.getName());
            subAcctRecharge.setUpOrderNumber(rechargeRes.getRechargeNo());
            subAcctRecharge.setCreateTime(DateUtil.date());
            subAcctRecharge.setUpdateTime(DateUtil.date());
            subAcctRecharge.setSubAcct(rechargeReq.getAip());
            subAcctRechargeRepository.save(subAcctRecharge);
        }
        return rechargeResResult;
    }

    /**
     * 充值到账查询
     *
     * @param rechargeQueryReq
     * @return
     */
    @Override
    public Result<RechargeQueryRes> RechargeQuery(RechargeQueryReq rechargeQueryReq) {
        String rechargeNo = rechargeQueryReq.getOrderNumber();
        SubAcctRecharge subAcctRecharge2 = subAcctRechargeRepository.findByOrdernumber("cz" + rechargeNo);
        /**
         * 无充值记录
         */
        if (subAcctRecharge2 == null) {
            return ResultBuilder.fail(ResponseResult.NOTRECHARGE);
        }
        return map.get(rechargeQueryReq.getChannel()).RechargeQuery(rechargeQueryReq);
    }

    /**
     * 单笔下发
     *
     * @param signleIssueReq
     * @return
     */
    @Override
    public Result<SingleIssueRes> SingleIssue(SingleIssueReq signleIssueReq) {
        Result<SingleIssueRes> singleIssueResResult = map.get(signleIssueReq.getChannel()).SingleIssue(signleIssueReq);
        if (StrUtil.equals(singleIssueResResult.getRetCode(), Constant.SUCCESS_CODE)) {
            SingleIssueRes singleIssueRes = singleIssueResResult.getBody();
            Trace trace = new Trace();
            trace.setTradeStatus("0");
            trace.setPayStatus("0");
            trace.setDeductionMoney(signleIssueReq.getAmount());
            trace.setIsNotifydw("0");
            trace.setUpOrderId(singleIssueRes.getBatchNo());
            trace.setOrderNumber("zz" + signleIssueReq.getAcctno());
            trace.setInAcctNo(signleIssueReq.getBanknum());
            ticketTraceRepository.save(trace);
        }
        return singleIssueResResult;
    }

    /**
     * 单笔下发查询
     *
     * @param singleIssueQueryReq
     * @return
     */
    @Override
    public Result<SingleIssueQueryRes> SingleIssueQuery(SingleIssueQueryReq singleIssueQueryReq) {
        Trace trace = ticketTraceRepository.findTracesByInAcctNo(singleIssueQueryReq.getAccountNo());
        if (trace != null) {
            String traceStatus = trace.getTradeStatus();
            if (StrUtil.equals(traceStatus, "1")) {
                SingleIssueQueryRes singleIssueQueryRes = new SingleIssueQueryRes();
                singleIssueQueryRes.setAmount(trace.getDeductionMoney());
                singleIssueQueryRes.setBankNum(trace.getInAcctNo());
                singleIssueQueryRes.setBankName("");
                singleIssueQueryRes.setTraceStatus(traceStatus);
                return ResultBuilder.success(singleIssueQueryRes);
            } else {
                return ResultBuilder.fail(ResponseResult.FAIL);
            }
        }
        return map.get(singleIssueQueryReq.getChannel()).SingleIssueQuery(singleIssueQueryReq);
    }
}
